package com.etheart.music.web.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.RequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.etheart.framework.core.dao.Page;
import com.etheart.framework.web.pager.Pager;
import com.etheart.framework.web.util.URLUtils;
import com.etheart.model.entity.Song;
import com.etheart.music.service.SongFullTextQueryService;
import com.opensymphony.xwork2.ActionSupport;

@Controller("searchAction")
@Scope("prototype")  
@ParentPackage("json-default")
public class SearchAction extends ActionSupport implements RequestAware{
	  
	private static final long serialVersionUID = 1L;
	@Autowired                            
	SongFullTextQueryService songFullTextQueryService;
	private String wd;//关键字                 
	private Pager<Song> pager = new Pager<Song>();//分页显示
	private Page<Song> page; 
	private List<Song> songs;
	
	@Action(value="s",results={@Result(name="success", location="/search/search.jsp")})  
	public String execute()  {
		jsearch();
		request.put("songs", songs);
		Pager.upset(request,page,wd,"/music/s.do");//初始化分页器
		return "success"; 
	}
	
	@Action(value="jsearch",results={@Result(type="json",name="jsearch",params={"excludeNullProperties","true","excludeProperties","songs.*.singer,songs.*.album,page.hibernate*"})})  
	public String jsearch()  {
		page = getSongPage();
		songs = page.getObjects();
		return "jsearch";
	}
	
	private Page<Song> getSongPage() {
		wd = URLUtils.urlDecode(wd);
		Page<Song>  page = songFullTextQueryService.fullTextQuerySongs(wd, pager.getOffset());
		return page;
	}
	
	Map<String, Object> request;
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	public String getWd() {
		return wd;
	}
	public void setWd(String wd) {
		this.wd = wd;
	}

	public void setPager(Pager<Song> pager) {
		this.pager = pager;
	}

	public List<Song> getSongs() {
		return songs;
	}


	public void setSongs(List<Song> songs) {
		this.songs = songs;
	}

}
